package com.ttg.web.task;

import com.ttg.common.dto.TaskAcctDTO;
import com.ttg.common.dto.TaskBaseDTO;
import com.ttg.common.dto.TaskCostGradingDTO;
import com.ttg.common.enums.IntervalTypeEnum;
import com.ttg.common.exception.BaseException;
import com.ttg.common.utils.JsonUtils;
import com.ttg.web.service.job.*;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobContext;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

/**
 * Description： 成本管理任务
 * Author: chenyou
 * Date: 2025/9/2 17:19
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Component
@EnableScheduling
@Slf4j
public class CostManageJobTask {

    @Autowired
    private CostManageJobService costManageJobService;

    @XxlJob("industryDepositByYear")
    @Async
    public ReturnT<String> industryDepositByYear() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask industryDepositByYear 行业存贷信息计算任务-按年】 start=>{}",param);
        try {
            TaskBaseDTO reqDto = new TaskBaseDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskBaseDTO.class);
            }
            costManageJobService.industryDepositByYear(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask industryDepositByYear 行业存贷信息计算任务-按年】 end");
        return ReturnT.SUCCESS;
    }

    @XxlJob("industryDepositByMonth")
    @Async
    public ReturnT<String> industryDepositByMonth() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask industryDepositByMonth 行业存贷信息计算任务-按月】 start=>{}",param);
        try {
            TaskBaseDTO reqDto = new TaskBaseDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskBaseDTO.class);
            }
            costManageJobService.industryDepositByMonth(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask industryDepositByMonth 行业存贷信息计算任务-按月】 end");
        return ReturnT.SUCCESS;
    }

    @XxlJob("merchDepositByYear")
    @Async
    public ReturnT<String> merchDepositByYear() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask merchDepositByYear 商户存贷信息计算任务-按年】 start=>{}",param);
        try {
            TaskBaseDTO reqDto = new TaskBaseDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskBaseDTO.class);
            }
            costManageJobService.merchDepositByYear(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask merchDepositByYear 商户存贷信息计算任务-按年】 end");
        return ReturnT.SUCCESS;
    }

    @XxlJob("merchDepositByMonth")
    @Async
    public ReturnT<String> merchDepositByMonth() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask merchDepositByMonth 商户存贷信息计算任务-按月】 start=>{}",param);
        try {
            TaskBaseDTO reqDto = new TaskBaseDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskBaseDTO.class);
            }
            costManageJobService.merchDepositByMonth(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask merchDepositByMonth 商户存贷信息计算任务-按月】 end");
        return ReturnT.SUCCESS;
    }

    @XxlJob("acctDepositAnalysisByYear")
    @Async
    public ReturnT<String> acctDepositAnalysisByYear() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask acctDepositAnalysisByYear 直绑/全量账户存款结构分解清算任务-按年】 start=>{}",param);
        try {
            TaskAcctDTO reqDto = new TaskAcctDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskAcctDTO.class);
            }
            reqDto.setIntervalType(IntervalTypeEnum.年度区间.getValue());
            costManageJobService.acctDepositAnalysisByYear(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask acctDepositAnalysisByYear 直绑/全量账户存款结构分解清算任务-按年】 end");
        return ReturnT.SUCCESS;
    }

    @XxlJob("acctDepositAnalysisByMonth")
    @Async
    public ReturnT<String> acctDepositAnalysisByMonth() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask acctDepositAnalysisByMonth 直绑/全量账户存款结构分解清算任务-按月】 start=>{}",param);
        try {
            TaskAcctDTO reqDto = new TaskAcctDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskAcctDTO.class);
            }
            reqDto.setIntervalType(IntervalTypeEnum.月度区间.getValue());
            costManageJobService.acctDepositAnalysisByMonth(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask acctDepositAnalysisByMonth 直绑/全量账户存款结构分解清算任务-按月】 end");
        return ReturnT.SUCCESS;
    }

    @XxlJob("acctComprehensiveAnalysisByYear")
    @Async
    public ReturnT<String> acctComprehensiveAnalysisByYear() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask acctComprehensiveAnalysisByYear 直绑/全量账户综合结构分解清算任务-按年】 start=>{}",param);
        try {
            TaskAcctDTO reqDto = new TaskAcctDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskAcctDTO.class);
            }
            reqDto.setIntervalType(IntervalTypeEnum.年度区间.getValue());
            costManageJobService.acctComprehensiveAnalysisByYear(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask acctComprehensiveAnalysisByYear 直绑/全量账户综合结构分解清算任务-按年】 end");
        return ReturnT.SUCCESS;
    }

    @XxlJob("acctComprehensiveAnalysisByMonth")
    @Async
    public ReturnT<String> acctComprehensiveAnalysisByMonth() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask acctComprehensiveAnalysisByMonth 直绑/全量账户综合结构分解清算任务-按月】 start=>{}",param);
        try {
            TaskAcctDTO reqDto = new TaskAcctDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskAcctDTO.class);
            }
            reqDto.setIntervalType(IntervalTypeEnum.月度区间.getValue());
            costManageJobService.acctComprehensiveAnalysisByMonth(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask acctComprehensiveAnalysisByMonth 直绑/全量账户综合结构分解清算任务-按月】 end");
        return ReturnT.SUCCESS;
    }


    @XxlJob("costGrading")
    @Async
    public ReturnT<String> costGrading() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask costGrading 商户成本分档任务】 start=>{}",param);
        try {
            TaskCostGradingDTO reqDto = new TaskCostGradingDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, TaskCostGradingDTO.class);
            }
            costManageJobService.costGrading(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask costGrading 商户成本分档任务】 end");
        return ReturnT.SUCCESS;
    }
}
